package com.tripupdater.view
{
	import com.tripupdater.event.TUEvent;
	import com.tripupdater.event.TUEventDispatcher;
	import com.tripupdater.model.Model;
	import com.tripupdater.model.Params;
	import com.tripupdater.type.CommandType;
	import com.tripupdater.vo.TwitterVO;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;

	[Event(name="twitterTimelineLoaded", type="com.tripupdater.event")]
	public class TwitterView extends MovieClip
	{
		private var model:Model;
		private var txtText:TextField;
		
		private var next:MovieClip;
		private var prev:MovieClip;
		private var all:MovieClip;
		
		private var currentTwitterIndex:int;
		
		public function TwitterView()
		{
			super();
			
			currentTwitterIndex= 0;
			
			model= Model.getInstance();
			
			createChildren();
		}
		
		private function createChildren():void
		{
			prev= getChildByName("prevInstance") as MovieClip;
			next= getChildByName("nextInstance") as MovieClip;
			all= getChildByName("seeAllInstance") as MovieClip;
			
			txtText= (getChildByName("twitterTextInstance") as MovieClip).getChildByName("txtTextInstance") as TextField;
			
			prev.visible= false;
			next.visible= false;
			all.visible= false;
			
			setupButton(prev);
			setupButton(next);
			setupButton(all);
		}
		
		public function getTwitterTimeline():void
		{
			TUEventDispatcher.getInstance().dispatchEvent(new TUEvent(CommandType.GET_TWITTER_UPDATE, null, this));
		}
		
		public function setTwitterTimeline():void
		{
			var twit:TwitterVO= model.twitterStatusTimeline[currentTwitterIndex] as TwitterVO;
			var html:String= "";
			html+= "<font color='#9F0908'>";
			html+= twit.text;
			html+= "</font><br />";
			html+= "<font color='#5E5E5E' size='9'>";
			html+= dateToString(twit.date) + " - " + model.getRelativeTime(twit.date);
			html+= "</font>";
			
			txtText.htmlText= html;
			
			if(currentTwitterIndex == 0){
				prev.visible= false;
			} else {
				prev.visible= true;
			}
			if(currentTwitterIndex >= (model.twitterStatusTimeline.length-1)){
				next.visible= false;
				all.visible= true;
			}
			if(currentTwitterIndex < (model.twitterStatusTimeline.length-1)){
				next.visible= true;
				all.visible= false;
			}
			
			dispatchEvent(new TUEvent(TUEvent.TWITTER_TIMELINE_LOADED));
		}
		
		private function mouseHandler(e:MouseEvent):void
		{
			if(e.target == prev){
				currentTwitterIndex--;
				if(currentTwitterIndex < 0) currentTwitterIndex= 0;
				setTwitterTimeline();
			} else
			if(e.target == next){
				currentTwitterIndex++;
				if(currentTwitterIndex == model.twitterStatusTimeline.length) currentTwitterIndex= (model.twitterStatusTimeline.length-1);
				setTwitterTimeline();
			} else
			if(e.target == all){
				navigateToURL(new URLRequest("http://twitter.com/" + Params.TWITTER_USER), "_blank");
			}
		}
		
		public function showErrorMessage():void
		{
			
		}
		
		private function setupButton(but:MovieClip):void
		{
			but.useHandCursor= true;
			but.buttonMode= true;
			but.mouseChildren= false;
			but.addEventListener(MouseEvent.CLICK, mouseHandler);
		}
		
		private function dateToString(date:Date):String
		{
			return ((date.getDate()+100).toString().substr(1) + "/" + (date.getMonth()+101).toString().substr(1) + "/" + date.getFullYear().toString() + " " + (date.getHours()+100).toString().substr(1) + ":" + (date.getMinutes()+100).toString().substr(1));
		}
	}
}